import { isArray } from '@/shared'
import { ShapeFlags } from '../shared/ShapeFlags'
export const Fragment = Symbol('Fragment')
export const Text = Symbol('Text')

export function createVNode(type, props?, children?) {
  const vnode = {
    type,
    props,
    children,
    component: null,
    key: props?.key,
    shapeFlag: getShapeFlag(type),
    el: null
  }
  if (typeof children === 'string') {
    vnode.shapeFlag |= ShapeFlags.TEXT_CHILDREN
  } else if (isArray(children)) {
    vnode.shapeFlag |= ShapeFlags.ARRAY_CHILDREN
  }

  // 组件+children,object
  if (vnode.shapeFlag & ShapeFlags.STATEFULCOMPONENT) {
    if (typeof children === 'object') {
      vnode.shapeFlag |= ShapeFlags.SLOTS_CHILDREN
    }
  }
  return vnode
}

function getShapeFlag(type) {
  return typeof type === 'string' ? ShapeFlags.ELEMENT : ShapeFlags.STATEFULCOMPONENT
}

export function createTextVNode(text: string) {
  return createVNode(Text, {}, text)
}
